﻿using Autofac;
using Equipment.API.Application.Service.DetectionmethodService;
using Equipment.API.Application.Service.OutgoinginspectionService;
using Equipment.API.Filter;
using Equipment.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Serilog;
using System.Reflection;

namespace Equipment.API.Extensions
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 添加服务到服务容器
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configuration">配置对象</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
        {
            // 注入中介者
            services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

            // 注入AutoMapper
            services.AddAutoMapper(typeof(MappingProfile));

            // 注册仓储
            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

            //注入全局异常处理
            services.AddControllers(x => x.Filters.Add<GlobalExceptionFilter>());


            // Configure Serilog
            // 配置 Serilog
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information() // 设置日志级别
                .WriteTo.File("logs/add.txt", rollingInterval: RollingInterval.Day) // 设置日志文件
                .CreateLogger();

            services.AddScoped<OutgoinginspectionServices>();
            services.AddScoped<InspectiondepartmentService>();
            services.AddScoped<InspectorsService>();
            services.AddScoped<DefectlocationService>();
            services.AddScoped<DefectgradeService>();
            services.AddScoped<DefecttypeService>();

            return services;

        }
        /// <summary>
        /// 注册 Autofac 模块
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection RegisterAutofac(this IServiceCollection services, ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterModule(new AutofacModuleRegister());
            return services;
        }




    }
}